package com.zhengtoudai.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhengtoudai.R;
import com.zhengtoudai.base.App;
import com.zhengtoudai.base.BaseActivity;
import com.zhengtoudai.global.Config;
import com.zhengtoudai.http.HttpUtil;
import com.zhengtoudai.injector.component.DaggerActivityComponent;
import com.zhengtoudai.injector.module.ActivityModule;
import com.zhengtoudai.ui.bean.TransferDetail;
import com.zhengtoudai.ui.presenter.ShengTenpayPresenterImpl;
import com.zhengtoudai.ui.presenter.TransferDetailPresenterImpl;
import com.zhengtoudai.ui.view.ShengTenpeyView;
import com.zhengtoudai.ui.view.TransferDetailView;
import com.zhengtoudai.util.AbToastUtil;
import com.zhengtoudai.util.Tool;
import com.zhengtoudai.view.MyProgressBar;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 债权认购界面
 */
public class TransferActivity extends BaseActivity implements TransferDetailView, ShengTenpeyView {


    @Bind(R.id.iv_img)
    ImageView mIvImg;
    @Bind(R.id.tv_year_rate)
    TextView mTvYearRate;
    @Bind(R.id.tv_transfer_principal)
    TextView mTvTransferPrincipal;
    @Bind(R.id.tv_time_limit)
    TextView mTvTimeLimit;
    @Bind(R.id.tv_repay_way)
    TextView mTvRepayWay;
    @Bind(R.id.tv_transfer_money)
    TextView mTvTransferMoney;
    @Bind(R.id.my_progressbar)
    MyProgressBar mMyProgressbar;
    @Bind(R.id.tv_available_balance)
    TextView mTvAvailableBalance;
    @Bind(R.id.btn_recharge)
    Button mBtnRecharge;
    @Bind(R.id.btn_investment)
    Button mBtnInvestment;
    @Bind(R.id.tv_investment_record)
    TextView mTvInvestmentRecord;
    @Bind(R.id.tv_project_detail)
    TextView mTvProjectDetail;

    @Inject
    TransferDetailPresenterImpl mPresenter;

    @Inject
    ShengTenpayPresenterImpl mTenpayPresenter;
    @Bind(R.id.tv_project_title)
    TextView mTvProjectTitle;
    @Bind(R.id.tv_invest_money)
    TextView mTvInvestMoney;
    private String borrowTransId;
    private TransferDetail result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transfer_detail);
        ButterKnife.bind(this);
    }

    @Override
    protected void initTitle() {
        titleUtil.setTitle("转让详情");
    }

    @Override
    protected void initView() {
        DaggerActivityComponent
                .builder()
                .appComponent(App.mApp.getAppComponent())
                .activityModule(new ActivityModule(this))
                .build()
                .inject(this);
        mPresenter.attachView(this);
        mTenpayPresenter.attachView(this);
        borrowTransId = getIntent().getStringExtra("borrowTransId");
        //borrowTransId = "YzDPh4GiToI[d]";
        //mPresenter.TransferDetail(borrowTransId);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (App.mApp.hasLogin()){
            mPresenter.TransferDetail(borrowTransId);
        }else{
            startActivity(LoginActivity.class);
            AbToastUtil.showToast(context,"请先登录");
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.detachView();
        mTenpayPresenter.detachView();
    }

    @OnClick({R.id.btn_recharge, R.id.btn_investment, R.id.tv_investment_record, R.id.tv_project_detail})
    public void onClick(View view) {
        if (Tool.isFastDoubleClick()) return;
        switch (view.getId()) {
            case R.id.btn_recharge:
                if (result!=null){
                    if (result.getRealnameStatus().equals("2")){
                        startActivity(RechargeActivity.class);
                    }else{
                        startActivity(RealNameCertificationActivity.class);
                        AbToastUtil.showToast(context,"请先绑定盛付通");
                    }
                }else{
                    AbToastUtil.showToast(context,"正在获取详情...");
                    mPresenter.TransferDetail(borrowTransId);
                }
                break;
            case R.id.btn_investment:
                Map<String, String> params = new ConcurrentHashMap<>();
                params.put("transferEid", borrowTransId);
                mTenpayPresenter.postShengTenpay("appBorrowLoggedCorrelation/saveAppTransferPrice.app", params);
                break;
            case R.id.tv_investment_record:
                Bundle record=new Bundle();
                record.putString("type","transId");
                record.putString("id",borrowTransId);
                startActivity(record,InvestmentRecordActivity.class);
                break;
            case R.id.tv_project_detail:
                Bundle bundle=new Bundle();
                bundle.putString("title","项目详情");
                bundle.putString("url", Config.BASE_URL+"web/borrow/borrowInfo/"+borrowTransId+".do");
                startActivity(bundle,WebViewActivity.class);
                break;
        }
    }

    @Override
    public void showDetailDialog() {
        showProgressDialog("正在获取详情...");
    }

    @Override
    public void onDetailFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context, e);
    }

    @Override
    public void onDetailResult(TransferDetail result) {
        this.result=result;
        Glide.with(this).load(Config.URL+result.getTransferBorrow().getImgUrl()).error(R.mipmap.xq001).into(mIvImg);
        mTvProjectTitle.setText(result.getTransferBorrow().getTransferTitle());
        mTvYearRate.setText(result.getTransferBorrow().getTransferInterestRate()+"%");
        mTvTransferPrincipal.setText("¥"+result.getTransferBorrow().getRepaying());//债权本金
        mTvTimeLimit.setText(result.getTransferBorrow().getEndTime()); //result.getTransferBorrow().getPublishDatetime()+"-"+

        mTvRepayWay.setText(result.getTransferBorrow().getRepaymentStyle());
        mTvTransferMoney.setText("¥"+result.getTransferBorrow().getTransferMoney());
        int status=result.getTransferBorrow().getTransferStatus();
        int progress;
        int backgroundResId;
        boolean isCheck;
        String btnText;
        if (status==0){
            btnText="等待审核";
            backgroundResId=R.drawable.shape_gray_button;
            progress=0;
            isCheck=false;
        }else if (status==1){
            btnText="认购中";
            backgroundResId=R.drawable.shape_red_button;
            progress=0;
            isCheck=true;
        }else if (status==2){
            btnText="转让成功";
            backgroundResId=R.drawable.shape_gray_button;
            progress=100;
            isCheck=false;
        }else if (status==3){ //3
            btnText="转让失败";
            backgroundResId=R.drawable.shape_gray_button;
            progress=0;
            isCheck=false;
        }else{ //4
            btnText="审核不通过";
            backgroundResId=R.drawable.shape_gray_button;
            progress=0;
            isCheck=false;
        }
        mBtnInvestment.setText(btnText);
        mBtnInvestment.setBackgroundResource(backgroundResId);
        mBtnInvestment.setClickable(isCheck);
        mMyProgressbar.setProgress(progress);

        mTvAvailableBalance.setText("¥"+result.getTransferBorrow().getAvailableMoney());
        mTvInvestMoney.setText(result.getTransferBorrow().getTransferMoney());
    }

    @Override
    public void dismissDetailDialog() {
        dismissProgressDialog();
    }

    @Override
    public void showDialog() {
        showProgressDialog("正在跳转中...");
    }

    @Override
    public void onFailure(Throwable e) {
        HttpUtil.disposeOnFailure(context, e);
    }

    @Override
    public void onResult(String html) {
        Bundle bundle = new Bundle();
        bundle.putString("title", "盛付通");
        bundle.putString("html", html);
        startActivity(bundle, WebViewActivity.class);
    }

    @Override
    public void dismissDialog() {
        dismissProgressDialog();
    }
}
